package njust.edu.dbutils;

import java.sql.*;

public class SimpleUtils {
    static private final String url = "jdbc:mysql://localhost/cater_system";
    static private final String username = "root";
    static private final String password  =  "123456";

    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");  //注册数据库驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {

            return DriverManager.getConnection(url+"?user="+username+"&password="+password+"&useUnicod \n" +
                    "e=true&characterEncoding=utf-8");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void release(Connection connection,
                               Statement statement, ResultSet resultSet) {

        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
